package com.xysq.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xysq.adapter.TalentFragmentAdapter;
import com.xysq.fragment.SkillFragment;
import com.xysq.fragment.TipsFragment;
import com.xysq.lemon.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class TalentActivity extends BaseActivity implements SkillFragment.OnFragmentInteractionListener, TipsFragment.OnFragmentInteractionListener, View.OnClickListener {

    @InjectView(R.id.viewpager_talent)
    ViewPager talentViewPager;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.layout_tips_indicater)
    RelativeLayout tipsIndicaterLayout;
    @InjectView(R.id.layout_skill_indicater)
    RelativeLayout skillIndicaterLayout;
    @InjectView(R.id.txt_fragment_skill)
    TextView fragmentSkillTxt;
    @InjectView(R.id.txt_fragment_tips)
    TextView fragmentTipsTxt;
    @InjectView(R.id.img_fragment_tips)
    ImageView fragmentTipsImg;
    @InjectView(R.id.img_fragment_skill)
    ImageView fragmentSkillImg;

    List<Fragment> fragmentList;
    TipsFragment tipsFragment;
    SkillFragment skillFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talent);
        initView();
    }

    private void initView() {
        fragmentList = new ArrayList<>();
        tipsFragment = TipsFragment.newInstance("this is", "tips");
        skillFragment = SkillFragment.newInstance("this is", "skill");
        fragmentList.add(tipsFragment);
        fragmentList.add(skillFragment);
        TalentFragmentAdapter talentFragmentAdapter = new TalentFragmentAdapter(getSupportFragmentManager(), fragmentList);
        talentViewPager.setAdapter(talentFragmentAdapter);
        tipsIndicaterLayout.setOnClickListener(this);
        skillIndicaterLayout.setOnClickListener(this);
        backIbtn.setOnClickListener(this);
        talentViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                Log.e("which i selected", i + "");
                if (i == 0) {
                    fragmentTipsTxt.setTextColor(Color.RED);
                    fragmentSkillTxt.setTextColor(getResources().getColor(R.color.txt_white));
                    fragmentTipsImg.setBackgroundColor(Color.RED);
                    fragmentSkillImg.setBackgroundColor(getResources().getColor(R.color.text_normal));
                }
                if (i == 1) {
                    fragmentTipsTxt.setTextColor(getResources().getColor(R.color.txt_white));
                    fragmentSkillTxt.setTextColor(Color.RED);
                    fragmentTipsImg.setBackgroundColor(getResources().getColor(R.color.text_normal));
                    fragmentSkillImg.setBackgroundColor(Color.RED);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.layout_tips_indicater:
                talentViewPager.setCurrentItem(0);
                break;
            case R.id.layout_skill_indicater:
                talentViewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void finish() {
        Intent intent = new Intent(this,MainTabActivity.class);
        intent.putExtra(MainTabActivity.INDEX,MainTabActivity.TAG_INDEX);
        startActivity(intent);
    }
}
